// 0 引入 用来发送请求的 方法 一定要把路径补全


Page({
    data: {
        // 轮播图数组
        swiperList: [],
        // 导航 数组
        cateList: [],
        // 楼层数据
        goodsList: [],
        imgUrl: [],
        currCate:7,
        indicatorDots: true, //显示面板显示点
        autoplay: false,  //自动切换
        interval: 5000,  //切换时间
        duration: 1000  //动画时长

    },
    // 页面开始加载 就会触发
    onLoad(options) {
        let that = this
        let getGoodsInfo = new Promise((resolve, reject) => {
            wx.request({
                url: "https://www.cbing.icu:8095/wxUser/getGoodsInfo",
                method: "GET",
                success(res) {
                    that.setData({
                        goodsList: res.data.data
                    })
                    resolve()
                }
            })
        })
        getGoodsInfo.then(() => {
            wx.request({
                url: "https://www.cbing.icu:8095/wxUser/getAllCate",
                method: "GET",
                success(res) {
                    that.setData({
                        cateList: res.data.data
                    })
                },
                complete(res) {
                    // console.log(that.getCurrList(that.data.cateList[0].id))
                    let list = that.getCurrList(that.data.cateList[0].id)
                    that.setData({
                        currList: list
                    })
                }
            })
        })


    },
    handleItemTap(e) {
        let id = e.target.dataset.index
        let list = this.getCurrList(id)
        console.log(list)
        this.setData({
            currList: list,
            currCate: id
        })
    },
    getCurrList(id){
        let list = []
        for (let item of this.data.goodsList) {
            if (item.categoryid === id) {
                list.push(item)
            }
        }
        return list
    },
    toDetail(e){
        let that = this
        console.log(e)
        wx.navigateTo({
            url:"../goods_list/index",
            success: (res)=> {
                // 通过 eventChannel 向被打开页面传送数据
                res.eventChannel.emit('acceptDataFromOpenerPage', { data: that.data.currList[e.currentTarget.dataset.index] })
            }
        })
    },
    addToCart(e) {
        let token = wx.getStorageSync("token")
        let that = this
        wx.showModal({
            title: 'Tips',
            content: '您确定要将此商品加入购物车吗',
            confirmText: '确定',
            cancelText: '取消',
            success: function (res) {
                if (res.cancel) {

                } else {
                    let formdata = '\r\n--XXX' +
                        '\r\nContent-Disposition: form-data; name="goodId"' +
                        '\r\n' +
                        '\r\n' + that.data.currList[e.currentTarget.dataset.index].id+
                        '\r\n--XXX' +
                        '\r\nContent-Disposition: form-data; name="num"' +
                        '\r\n' +
                        '\r\n1' +
                        '\r\n--XXX--'
                    wx.request({

                        url: "https://www.cbing.icu:8095/wxUser/addToShopCart",
                        method: 'POST',
                        header: {
                            'content-type': 'multipart/form-data;boundary=XXX',
                            'Authorization':token
                        },
                        data: formdata,
                        success(res) {

                            wx.showToast({
                                icon: 'none',
                                title: res.data.message,
                                duration: 2000

                            })

                        }
                    })
                }
            }
        })
    },
})
